package com.windoent.yugioh.ui.activity.some;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.baidu.mobstat.StatService;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.utils.ImageUtil;
import com.windoent.yugioh.utils.SaveBitmapGalleryUtils;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.ToastUtils;

import io.reactivex.functions.Consumer;

/**
 * author : hp
 * date   : 2020/4/11 23:07
 * desc   :PhotoView图片响应手指
 */
public class PhotoViewActivity extends BaseActivity {
    private PhotoView mPhotoview;
    private ImageView mIvSavePic;
    private String h5url;

    @Override
    public int getLayout() {
        return R.layout.activivty_photoview;
    }

    @Override
    public void initView() {
        immersiveStatusBar();
        mPhotoview = (PhotoView) findViewById(R.id.photoview);
        mIvSavePic = (ImageView) findViewById(R.id.iv_save_pic);

    }

    @Override
    public void initData() {
        super.initData();
        Intent intent = getIntent();
        //h5图片
        h5url = intent.getStringExtra(Constants.PHOTOURL);
        if (!TextUtils.isEmpty(h5url))
            SimpleUtils.loadImageForView(PhotoViewActivity.this, mPhotoview, h5url, 0);
        mIvSavePic.setVisibility(View.GONE);
    }

    @Override
    public void initListener() {
        super.initListener();
        mIvSavePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxPermission();
            }
        });
        mPhotoview.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                finish();
            }
        });
    }

    //权限申请
    @SuppressLint("CheckResult")
    private void rxPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            if (!TextUtils.isEmpty(h5url)) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        SaveBitmapGalleryUtils utils = new SaveBitmapGalleryUtils();
                                        Bitmap bitmap = ImageUtil.getUrlBitMap(h5url);
                                        boolean save = utils.addJpgSignatureToGallery(PhotoViewActivity.this, bitmap, "feiyu");
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtils.showText(save ? getString(R.string.string_save_gallery_success) : getString(R.string.string_save_gallery_fail));
                                            }
                                        });
                                    }
                                }).start();
                            }
                        }
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "图片查看界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "图片查看界面");
    }
}

